#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <stdint.h>
#include <map>
#include <vector>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class QTreeWidgetItem;

struct sData
{
    int64_t t;
    int value;
	float fval;
    int status;
    int cot;
    int addr;
    int64_t timestamp;
    int direct;

    /**
     * @brief 数据结构体的默认构造函数
     *
     * 初始化数据结构体的成员变量:
     * - t: 时间戳,初始化为0
     * - value: 数值,初始化为0
     * - fval: 浮点值,初始化为0.0
     * - cot: 传送原因,初始化为-1
     * - addr: 地址,初始化为-1
     * - time: 时间,初始化为0
     */
    sData()
    {
        t = 0;
        value = 0;
        fval = 0.0;
		cot = -1;
		addr = -1;
        timestamp = 0;
        direct = -1;
    }
};

struct sRecieverHandleParameter
{
    int typeID;
    int COT;
    std::vector<sData> vtData;
	std::vector<sData> vtSOE;

    sRecieverHandleParameter()
    {
        typeID = -1;
        COT = -1;
        vtData.clear();
		vtSOE.clear();
    }
};


class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

protected slots:
    int on_toolButton_clicked(bool checked);
    int on_treeWidget_itemDoubleClicked(QTreeWidgetItem* item, int column);
    int listPoint(QTreeWidgetItem* item, int column);
    int showTable(QTreeWidgetItem* item, int column);
    void on_comboBox_currentIndexChanged(const QString& text);
    void on_checkBox_clicked(bool checked);

private:
	int parseIEC104(uint8_t* buffer, int length,int direct);
    int translateIEC104(uint8_t* buffer, int msgSize);
	int process_line(QString ss);
	int process_buff(QByteArray &tmp_buff, QByteArray &buff, int direct);
    int createTree();

private:
    Ui::MainWindow *ui;

    std::map<int, int> _mapTypeNum;
    std::map<int, int> _mapCOTNum;
    std::vector<int> _vtType;
    std::map<int, std::map<int, std::vector<sData>>> _mapAll; //map<type, map<addr, vector<data>>>
	std::map<int, std::map<int, int>> _mapAllChange; //map<type, map<addr, change>>

    int64_t _timestamp;
	QByteArray _tmp_buff_recv;
	QByteArray _tmp_buff_send;
	int _recv_or_send;
};
#endif // MAINWINDOW_H
